package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.MallActivityShare;
import com.sc.nft.entity.MallActivityShareTeam;
import com.sc.nft.entity.dto.AdminMallActivitySharePageDTO;
import com.sc.nft.entity.dto.MallActivityShareDetailDTO;
import com.sc.nft.entity.dto.MallActivitySharePageDTO;
import com.sc.nft.mapper.MallActivityShareMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 分享购活动DAO
 *
 * @author liGen
 * @version 1.0
 * @date 2023/12/11 2:58 下午
 */
@Slf4j
@Repository
public class MallActivityShareDao extends ServiceImpl<MallActivityShareMapper, MallActivityShare> {

    /**
     * 后台分享购活动列表
     *
     * @param pageNo
     * @param pageSize
     * @param productName
     * @param daoId
     * @param status
     * @return
     */
    public Page<AdminMallActivitySharePageDTO> getAdminMallActivitySharePageDTO(Integer pageNo, Integer pageSize, String productName, Long daoId, Boolean status) {
        return baseMapper.getAdminMallActivitySharePageDTO(new Page(pageNo, pageSize), productName, daoId, status);
    }

    /**
     * 分享购活动列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<MallActivitySharePageDTO> getMallActivitySharePageDTO(Integer pageNo, Integer pageSize) {
        return baseMapper.getMallActivitySharePageDTO(new Page(pageNo, pageSize));
    }

    /**
     * 后台分享购活动详情DTO
     *
     * @param id
     * @return
     */
    public MallActivityShareDetailDTO getAdminMallActivityShareDetailDTO(Long id) {
        return baseMapper.getMallActivityShareDetailDTO(id);
    }

    /**
     * 修改后台分享活动上下架状态
     *
     * @param id
     * @param status
     * @return
     */
    public boolean updateStatus(Long id, Boolean status) {
        UpdateWrapper<MallActivityShare> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("status", status);
        return update(new MallActivityShare(), wrapper);
    }

    /**
     * 修改后台分享活动排序
     *
     * @param id
     * @param sort
     * @return
     */
    public boolean updateSort(Long id, Integer sort) {
        UpdateWrapper<MallActivityShare> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("sort", sort);
        return update(new MallActivityShare(), wrapper);
    }

    /**
     * 判断活动商品是否拥有拼团活动
     *
     * @param activityId
     * @return
     */
    public Boolean hasShareByActivityId(Long activityId){
        LambdaQueryWrapper<MallActivityShare> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallActivityShare::getActivityId, activityId);
        wrapper.eq(MallActivityShare::getStatus, true);
        Integer result = baseMapper.selectCount(wrapper);
        return null != result && result > 0;
    }

    /**
     * 判断活动商品是否拥有拼团活动
     *
     * @param activityId
     * @return
     */
    public List<MallActivityShare> getListByActivityId(Long activityId){
        LambdaQueryWrapper<MallActivityShare> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallActivityShare::getActivityId, activityId);
        wrapper.eq(MallActivityShare::getStatus, true);
        return baseMapper.selectList(wrapper);
    }
}
